package 面向对象;

public class 递归
{
	public static void main(String[] args){
		System.out.println(jiecheng1(5));
		System.out.println(jiecheng2(6));
		
	}
	//递归实现阶乘
	//递归必须有出口，否则内存溢出
	//递归适用层级比较小的，吃内存大
	public static int jiecheng2(int num){
		if(num==0)return 1;
		if(num==1)return 1;
		return num*jiecheng2(num-1);
	}
	//循环实现阶乘
	public static int jiecheng1(int num){
		int result=num;
		int i=num-1;
		do{
			result=result*i;
			i--;
		}while(i>1);
		return result;
	}
}
